home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / bin / xdg-mime < prev    next >
Encoding:
Text File  |  2010-09-15  |  29.5 KB  |  1,154 lines

  1. #!/bin/sh
  2. #---------------------------------------------
  3. #   xdg-mime
  4. #
  5. #   Utility script to manipulate MIME related information
  6. #   on XDG compliant systems.
  7. #
  8. #   Refer to the usage() function below for usage.
  9. #
  10. #   Copyright 2006, Kevin Krammer <kevin.krammer@gmx.at>
  11. #   Copyright 2006, Jeremy White <jwhite@codeweavers.com>
  12. #
  13. #   LICENSE:
  14. #
  15. #   Permission is hereby granted, free of charge, to any person obtaining a
  16. #   copy of this software and associated documentation files (the "Software"),
  17. #   to deal in the Software without restriction, including without limitation
  18. #   the rights to use, copy, modify, merge, publish, distribute, sublicense,
  19. #   and/or sell copies of the Software, and to permit persons to whom the
  20. #   Software is furnished to do so, subject to the following conditions:
  21. #
  22. #   The above copyright notice and this permission notice shall be included
  23. #   in all copies or substantial portions of the Software.
  24. #
  25. #   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  26. #   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  27. #   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  28. #   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  29. #   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  30. #   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  31. #   OTHER DEALINGS IN THE SOFTWARE.
  32. #
  33. #---------------------------------------------
  34.  
  35. manualpage()
  36. {
  37. cat << _MANUALPAGE
  38. Name
  39.  
  40. xdg-mime - command line tool for querying information about file type handling
  41. and adding descriptions for new file types
  42.  
  43. Synopsis
  44.  
  45. xdg-mime query { filetype | default } ...
  46.  
  47. xdg-mime default application mimetype(s)
  48.  
  49. xdg-mime install [--mode mode] [--novendor] mimetypes-file
  50.  
  51. xdg-mime uninstall [--mode mode] mimetypes-file
  52.  
  53. xdg-mime { --help | --manual | --version }
  54.  
  55. Description
  56.  
  57. The xdg-mime program can be used to query information about file types and to
  58. add descriptions for new file types.
  59.  
  60. Commands
  61.  
  62. query
  63.  
  64.     Returns information related to file types.
  65.  
  66.     The query option is for use inside a desktop session only. It is not
  67.     recommended to use xdg-mime query as root.
  68.  
  69.     The following queries are supported:
  70.  
  71.     query filetype FILE: Returns the file type of FILE in the form of a MIME
  72.     type.
  73.  
  74.     query default mimetype: Returns the default application that the desktop
  75.     environment uses for opening files of type mimetype. The default
  76.     application is identified by its *.desktop file.
  77.  
  78. default
  79.  
  80.     Ask the desktop environment to make application the default application for
  81.     opening files of type mimetype. An application can be made the default for
  82.     several file types by specifying multiple mimetypes.
  83.  
  84.     application is the desktop file id of the application and has the form
  85.     vendor-name.desktop application must already be installed in the desktop
  86.     menu before it can be made the default handler. The aplication's desktop
  87.     file must list support for all the MIME types that it wishes to be the
  88.     default handler for.
  89.  
  90.     Requests to make an application a default handler may be subject to system
  91.     policy or approval by the end-user. xdg-mime query can be used to verify
  92.     whether an application is the actual default handler for a specific file
  93.     type.
  94.  
  95.     The default option is for use inside a desktop session only. It is not
  96.     recommended to use xdg-mime default as root.
  97.  
  98. install
  99.     Adds the file type descriptions provided in mimetypes-file to the desktop
  100.     environment. mimetypes-file must be a XML file that follows the
  101.     freedesktop.org Shared MIME-info Database specification and that has a
  102.     mime-info element as its document root. For each new file type one or more
  103.     icons with name major-minor must be installed with the xdg-icon-resource
  104.     command in the mimetypes context. For example the application/
  105.     vnd.oasis.opendocument.text filetype requires an icon by the name of
  106.     application-vnd.oasis.opendocument.text to be installed.
  107. uninstall
  108.     Removes the file type descriptions provided in mimetypes-file and
  109.     previously added with xdg-mime install from the desktop environment.
  110.     mimetypes-file must be a XML file that follows the freedesktop.org Shared
  111.     MIME-info Database specification and that has a mime-info element as its
  112.     document root.
  113.  
  114. Options
  115.  
  116. --mode mode
  117.  
  118.     mode can be user or system. In user mode the file is (un)installed for the
  119.     current user only. In system mode the file is (un)installed for all users
  120.     on the system. Usually only root is allowed to install in system mode.
  121.  
  122.     The default is to use system mode when called by root and to use user mode
  123.     when called by a non-root user.
  124.  
  125. --novendor
  126.  
  127.     Normally, xdg-mime checks to ensure that the mimetypes-file to be installed
  128.     has a proper vendor prefix. This option can be used to disable that check.
  129.  
  130.     A vendor prefix consists of alpha characters ([a-zA-Z]) and is terminated
  131.     with a dash ("-"). Companies and organizations are encouraged to use a word
  132.     or phrase, preferably the organizations name, for which they hold a
  133.     trademark as their vendor prefix. The purpose of the vendor prefix is to
  134.     prevent name conflicts.
  135.  
  136. --help
  137.     Show command synopsis.
  138. --manual
  139.     Show this manualpage.
  140. --version
  141.     Show the xdg-utils version information.
  142.  
  143. Environment Variables
  144.  
  145. xdg-mime honours the following environment variables:
  146.  
  147. XDG_UTILS_DEBUG_LEVEL
  148.     Setting this environment variable to a non-zero numerical value makes
  149.     xdg-mime do more verbose reporting on stderr. Setting a higher value
  150.     increases the verbosity.
  151. XDG_UTILS_INSTALL_MODE
  152.     This environment variable can be used by the user or administrator to
  153.     override the installation mode. Valid values are user and system.
  154.  
  155. Exit Codes
  156.  
  157. An exit code of 0 indicates success while a non-zero exit code indicates
  158. failure. The following failure codes can be returned:
  159.  
  160. 1
  161.     Error in command line syntax.
  162. 2
  163.     One of the files passed on the command line did not exist.
  164. 3
  165.     A required tool could not be found.
  166. 4
  167.     The action failed.
  168. 5
  169.     No permission to read one of the files passed on the command line.
  170.  
  171. See Also
  172.  
  173. xdg-icon-resource(1), xdg-desktop-menu(1)
  174.  
  175. Examples
  176.  
  177. xdg-mime query filetype /tmp/foobar.png
  178.  
  179. Prints the MIME type of the file /tmp/foobar.png, in this case image/png
  180.  
  181. xdg-mime query default image/png
  182.  
  183. Prints the .desktop filename of the application which is registered to open PNG
  184. files.
  185.  
  186. xdg-mime install shinythings-shiny.xml
  187.  
  188. Adds a file type description for "shiny"-files. "shinythings-" is used as the
  189. vendor prefix. The file type description could look as folows.
  190.  
  191. shinythings-shiny.xml:
  192.  
  193. <?xml version="1.0"?>
  194. <mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'>
  195.   <mime-type type="text/x-shiny">
  196.     <comment>Shiny new file type</comment>
  197.     <glob pattern="*.shiny"/>
  198.     <glob pattern="*.shi"/>
  199.   </mime-type>
  200. </mime-info>
  201.  
  202. An icon for this new file type must also be installed, for example with:
  203.  
  204. xdg-icon-resource install --context mimetypes --size 64 shiny-file-icon.png text-x-shiny
  205.  
  206. _MANUALPAGE
  207. }
  208.  
  209. usage()
  210. {
  211. cat << _USAGE
  212. xdg-mime - command line tool for querying information about file type handling
  213. and adding descriptions for new file types
  214.  
  215. Synopsis
  216.  
  217. xdg-mime query { filetype | default } ...
  218.  
  219. xdg-mime default application mimetype(s)
  220.  
  221. xdg-mime install [--mode mode] [--novendor] mimetypes-file
  222.  
  223. xdg-mime uninstall [--mode mode] mimetypes-file
  224.  
  225. xdg-mime { --help | --manual | --version }
  226.  
  227. _USAGE
  228. }
  229.  
  230. #@xdg-utils-common@
  231.  
  232. #----------------------------------------------------------------------------
  233. #   Common utility functions included in all XDG wrapper scripts
  234. #----------------------------------------------------------------------------
  235.  
  236. DEBUG()
  237. {
  238.   [ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && return 0;
  239.   [ ${XDG_UTILS_DEBUG_LEVEL} -lt $1 ] && return 0;
  240.   shift
  241.   echo "$@" >&2
  242. }
  243.  
  244. #-------------------------------------------------------------
  245. # Exit script on successfully completing the desired operation
  246.  
  247. exit_success()
  248. {
  249.     if [ $# -gt 0 ]; then
  250.         echo "$@"
  251.         echo
  252.     fi
  253.  
  254.     exit 0
  255. }
  256.  
  257.  
  258. #-----------------------------------------
  259. # Exit script on malformed arguments, not enough arguments
  260. # or missing required option.
  261. # prints usage information
  262.  
  263. exit_failure_syntax()
  264. {
  265.     if [ $# -gt 0 ]; then
  266.         echo "xdg-mime: $@" >&2
  267.         echo "Try 'xdg-mime --help' for more information." >&2
  268.     else
  269.         usage
  270.         echo "Use 'man xdg-mime' or 'xdg-mime --manual' for additional info."
  271.     fi
  272.  
  273.     exit 1
  274. }
  275.  
  276. #-------------------------------------------------------------
  277. # Exit script on missing file specified on command line
  278.  
  279. exit_failure_file_missing()
  280. {
  281.     if [ $# -gt 0 ]; then
  282.         echo "xdg-mime: $@" >&2
  283.     fi
  284.  
  285.     exit 2
  286. }
  287.  
  288. #-------------------------------------------------------------
  289. # Exit script on failure to locate necessary tool applications
  290.  
  291. exit_failure_operation_impossible()
  292. {
  293.     if [ $# -gt 0 ]; then
  294.         echo "xdg-mime: $@" >&2
  295.     fi
  296.  
  297.     exit 3
  298. }
  299.  
  300. #-------------------------------------------------------------
  301. # Exit script on failure returned by a tool application
  302.  
  303. exit_failure_operation_failed()
  304. {
  305.     if [ $# -gt 0 ]; then
  306.         echo "xdg-mime: $@" >&2
  307.     fi
  308.  
  309.     exit 4
  310. }
  311.  
  312. #------------------------------------------------------------
  313. # Exit script on insufficient permission to read a specified file
  314.  
  315. exit_failure_file_permission_read()
  316. {
  317.     if [ $# -gt 0 ]; then
  318.         echo "xdg-mime: $@" >&2
  319.     fi
  320.  
  321.     exit 5
  322. }
  323.  
  324. #------------------------------------------------------------
  325. # Exit script on insufficient permission to write a specified file
  326.  
  327. exit_failure_file_permission_write()
  328. {
  329.     if [ $# -gt 0 ]; then
  330.         echo "xdg-mime: $@" >&2
  331.     fi
  332.  
  333.     exit 6
  334. }
  335.  
  336. check_input_file()
  337. {
  338.     if [ ! -e "$1" ]; then
  339.         exit_failure_file_missing "file '$1' does not exist"
  340.     fi
  341.     if [ ! -r "$1" ]; then
  342.         exit_failure_file_permission_read "no permission to read file '$1'"
  343.     fi
  344. }
  345.  
  346. check_vendor_prefix()
  347. {
  348.     file_label="$2"
  349.     [ -n "$file_label" ] || file_label="filename"
  350.     file=`basename "$1"`
  351.     case "$file" in
  352.        [a-zA-Z]*-*)
  353.          return
  354.          ;;
  355.     esac
  356.  
  357.     echo "xdg-mime: $file_label '$file' does not have a proper vendor prefix" >&2
  358.     echo 'A vendor prefix consists of alpha characters ([a-zA-Z]) and is terminated' >&2
  359.     echo 'with a dash ("-"). An example '"$file_label"' is '"'example-$file'" >&2
  360.     echo "Use --novendor to override or 'xdg-mime --manual' for additional info." >&2
  361.     exit 1
  362. }
  363.  
  364. check_output_file()
  365. {
  366.     # if the file exists, check if it is writeable
  367.     # if it does not exists, check if we are allowed to write on the directory
  368.     if [ -e "$1" ]; then
  369.         if [ ! -w "$1" ]; then
  370.             exit_failure_file_permission_write "no permission to write to file '$1'"
  371.         fi
  372.     else
  373.         DIR=`dirname "$1"`
  374.         if [ ! -w "$DIR" -o ! -x "$DIR" ]; then
  375.             exit_failure_file_permission_write "no permission to create file '$1'"
  376.         fi
  377.     fi
  378. }
  379.  
  380. #----------------------------------------
  381. # Checks for shared commands, e.g. --help
  382.  
  383. check_common_commands()
  384. {
  385.     while [ $# -gt 0 ] ; do
  386.         parm="$1"
  387.         shift
  388.  
  389.         case "$parm" in
  390.             --help)
  391.             usage
  392.             echo "Use 'man xdg-mime' or 'xdg-mime --manual' for additional info."
  393.             exit_success
  394.             ;;
  395.  
  396.             --manual)
  397.             manualpage
  398.             exit_success
  399.             ;;
  400.  
  401.             --version)
  402.             echo "xdg-mime 1.0.2"
  403.             exit_success
  404.             ;;
  405.         esac
  406.     done
  407. }
  408.  
  409. check_common_commands "$@"
  410.  
  411. [ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && unset XDG_UTILS_DEBUG_LEVEL;
  412. if [ ${XDG_UTILS_DEBUG_LEVEL-0} -lt 1 ]; then
  413.     # Be silent
  414.     xdg_redirect_output=" > /dev/null 2> /dev/null"
  415. else
  416.     # All output to stderr
  417.     xdg_redirect_output=" >&2"
  418. fi
  419.  
  420. #--------------------------------------
  421. # Checks for known desktop environments
  422. # set variable DE to the desktop environments name, lowercase
  423.  
  424. detectDE()
  425. {
  426.     if [ x"$KDE_FULL_SESSION" = x"true" ]; then DE=kde;
  427.     elif [ x"$GNOME_DESKTOP_SESSION_ID" != x"" ]; then DE=gnome;
  428.     elif `dbus-send --print-reply --dest=org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.GetNameOwner string:org.gnome.SessionManager > /dev/null 2>&1` ; then DE=gnome;
  429.     elif xprop -root _DT_SAVE_MODE 2> /dev/null | grep ' = \"xfce4\"$' >/dev/null 2>&1; then DE=xfce;
  430.     fi
  431. }
  432.  
  433. #----------------------------------------------------------------------------
  434. # kfmclient exec/openURL can give bogus exit value in KDE <= 3.5.4
  435. # It also always returns 1 in KDE 3.4 and earlier
  436. # Simply return 0 in such case
  437.  
  438. kfmclient_fix_exit_code()
  439. {
  440.     version=`kde${KDE_SESSION_VERSION}-config --version 2>/dev/null | grep KDE`
  441.     major=`echo $version | sed 's/KDE: \([0-9]\).*/\1/'`
  442.     minor=`echo $version | sed 's/KDE: [0-9]*\.\([0-9]\).*/\1/'`
  443.     release=`echo $version | sed 's/KDE: [0-9]*\.[0-9]*\.\([0-9]\).*/\1/'`
  444.     test "$major" -gt 3 && return $1
  445.     test "$minor" -gt 5 && return $1
  446.     test "$release" -gt 4 && return $1
  447.     return 0
  448. }
  449.  
  450. update_mime_database()
  451. {
  452.    if [ x"$mode" = x"user" -a -n "$DISPLAY" ] ; then
  453.       detectDE
  454.       if [ x"$DE" = x"kde" ] ; then
  455.          DEBUG 1 "Running kbuildsycoca"
  456.          if [ x"$KDE_SESSION_VERSION" = x"4" ]; then
  457.              eval 'kbuildsycoca4'$xdg_redirect_output
  458.          else
  459.              eval 'kbuildsycoca'$xdg_redirect_output
  460.          fi
  461.       fi
  462.    fi
  463.    for x in `echo "$PATH:/opt/gnome/bin" | sed 's/:/ /g'`; do
  464.       if [ -x $x/update-mime-database ] ; then
  465.          DEBUG 1 "Running $x/update-mime-database $1"
  466.          eval '$x/update-mime-database $1'$xdg_redirect_output
  467.          return
  468.       fi
  469.    done
  470. }
  471.  
  472. info_kde()
  473. {
  474.     if [ x"$KDE_SESSION_VERSION" = x"4" ]; then
  475.         DEBUG 1 "Running kmimetypefinder \"$1\""
  476.         KMIMETYPEFINDER=`which kmimetypefinder 2>/dev/null`
  477.         $KMIMETYPEFINDER "$1" 2>/dev/null | head -n 1
  478.     else
  479.         DEBUG 1 "Running kfile \"$1\""
  480.         KFILE=`which kfile 2>/dev/null`
  481.         $KFILE "$1" 2> /dev/null | head -n 1 | cut -d "(" -f 2 | cut -d ")" -f 1
  482.     fi
  483.  
  484.     if [ $? -eq 0 ]; then
  485.         exit_success
  486.     else
  487.         exit_failure_operation_failed
  488.     fi
  489. }
  490.  
  491. info_gnome()
  492. {
  493.     file=`readlink -f "$1"` # Normalize path
  494.  
  495.     if gvfs-info --help 2>/dev/null 1>&2; then
  496.         DEBUG 1 "Running gvfs-info \"$file\""
  497.         gvfs-info "$file" 2> /dev/null | grep standard::content-type | cut -d' ' -f4
  498.     else
  499.        DEBUG 1 "Running gnomevfs-info \"$file\""
  500.        gnomevfs-info "$file" 2> /dev/null | grep "^MIME" | cut -d ":" -f 2 | sed s/"^ "//
  501.     fi
  502.  
  503.     if [ $? -eq 0 ]; then
  504.         exit_success
  505.     else
  506.         exit_failure_operation_failed
  507.     fi
  508. }
  509.  
  510. info_generic()
  511. {
  512.     DEBUG 1 "Running file -i \"$1\""
  513.     /usr/bin/file -i "$1" 2> /dev/null | cut -d ":" -f 2 | sed s/"^ "//
  514.  
  515.     if [ $? -eq 0 ]; then
  516.         exit_success
  517.     else
  518.         exit_failure_operation_failed
  519.     fi
  520. }
  521.  
  522. make_default_kde()
  523. {
  524.     # $1 is vendor-name.desktop
  525.     # $2 is mime/type
  526.     #
  527.     # On KDE 3, add to $KDE_CONFIG_PATH/profilerc:
  528.     # [$2 - 1]
  529.     # Application=$1
  530.     #
  531.     # Remove all [$2 - *] sections, or even better,
  532.     # renumber [$2 - *] sections and remove duplicate
  533.     #
  534.     # On KDE 4, add $2=$1 to $XDG_DATA_APPS/mimeapps.list
  535.     #
  536.     # Example file:
  537.     #
  538.     # [Added Associations]
  539.     # text/plain=kde4-kate.desktop;kde4-kwrite.desktop;
  540.     #
  541.     # [Removed Associations]
  542.     # text/plain=gnome-gedit.desktop;gnu-emacs.desktop;
  543.     vendor="$1"
  544.     mimetype="$2"
  545.     if [ x"$KDE_SESSION_VERSION" = x"4" ]; then
  546.         default_dir=`kde4-config --path xdgdata-apps 2> /dev/null | cut -d ':' -f 1`
  547.         default_file="$default_dir/mimeapps.list"
  548.     else
  549.         default_dir=`kde-config --path config 2> /dev/null | cut -d ':' -f 1`
  550.         default_file="$default_dir/profilerc"
  551.     fi
  552.     if [ -z "$default_dir" ]; then
  553.         DEBUG 2 "make_default_kde: No kde runtime detected"
  554.         return
  555.     fi
  556.     DEBUG 2 "make_default_kde $vendor $mimetype"
  557.     DEBUG 1 "Updating $default_file"
  558.     mkdir -p "$default_dir"
  559.     [ -f $default_file ] || touch $default_file
  560.     if [ x"$KDE_SESSION_VERSION" = x"4" ]; then
  561.         # FIXME update the required section "[Added Associations]"
  562.         DEBUG 1 "NOT IMPLEMENTED YET"
  563.     else
  564.         awk -v application="$vendor" -v mimetype="$mimetype" '
  565.     BEGIN {
  566.         header_start="[" mimetype " - "
  567.         supress=0
  568.     }
  569.     {
  570.         if (index($0, header_start) == 1 )
  571.             supress=1
  572.         else
  573.             if (/^\[/) { supress=0 }
  574.  
  575.         if (!supress) {
  576.             print $0
  577.         }
  578.     }
  579.     END {
  580.         print ""
  581.         print "[" mimetype " - 1]"
  582.         print "Application=" application
  583.         print "AllowAsDefault=true"
  584.         print "GenericServiceType=Application"
  585.         print "Preference=1"
  586.         print "ServiceType=" mimetype
  587.     }
  588. ' $default_file > ${default_file}.new && mv ${default_file}.new $default_file
  589.     fi
  590. }
  591.  
  592. make_default_generic()
  593. {
  594.     # $1 is vendor-name.desktop
  595.     # $2 is mime/type
  596.     # Add $2=$1 to XDG_DATA_HOME/applications/defaults.list
  597.     xdg_user_dir="$XDG_DATA_HOME"
  598.     [ -n "$xdg_user_dir" ] || xdg_user_dir="$HOME/.local/share"
  599.     default_file="$xdg_user_dir/applications/defaults.list"
  600.     DEBUG 2 "make_default_generic $1 $2"
  601.     DEBUG 1 "Updating $default_file"
  602.     grep -v "$2=" $default_file > ${default_file}.new 2> /dev/null
  603.     if ! grep "[Default Applications]" ${default_file}.new > /dev/null; then
  604.        echo "[Default Applications]" >> ${default_file}.new
  605.     fi
  606.     echo $2=$1 >> ${default_file}.new
  607.     mv ${default_file}.new $default_file
  608. }
  609.  
  610. defapp_generic()
  611. {
  612.     MIME="$1"
  613.     xdg_user_dir="$XDG_DATA_HOME"
  614.     [ -n "$xdg_user_dir" ] || xdg_user_dir="$HOME/.local/share"
  615.     xdg_user_dir="$xdg_user_dir/$xdg_dir_name"
  616.     xdg_system_dirs="$XDG_DATA_DIRS"
  617.     [ -n "$xdg_system_dirs" ] || xdg_system_dirs=/usr/local/share/:/usr/share/
  618.  
  619.     for x in `echo "$xdg_user_dir:$xdg_system_dirs" | sed 's/:/ /g'`; do
  620.        DEBUG 2 "Checking $x/applications/defaults.list"
  621.        trader_result=`grep "$MIME=" $x/applications/defaults.list 2> /dev/null | cut -d '=' -f 2 | cut -d ';' -f 1`
  622.        if [ -n "$trader_result" ] ; then
  623.           echo $trader_result
  624.           exit_success
  625.        fi
  626.     done
  627.     exit_success
  628. }
  629.  
  630. defapp_kde()
  631. {
  632.     MIME="$1"
  633.     if [ x"$KDE_SESSION_VERSION" = x"4" ]; then
  634.         KTRADER=`which ktraderclient 2> /dev/null`
  635.         MIMETYPE="--mimetype"
  636.         SERVICETYPE="--servicetype"
  637.     else
  638.         KTRADER=`which ktradertest 2> /dev/null`
  639.     fi
  640.     if [ -n "$KTRADER" ] ; then
  641.         DEBUG 1 "Running KDE trader query \"$MIME\" mimetype and \"Application\" servicetype"
  642.         trader_result=`$KTRADER $MIMETYPE "$MIME" $SERVICETYPE Application 2>/dev/null \
  643.             | grep DesktopEntryPath | head -n 1 | cut -d ':' -f 2 | cut -d \' -f 2`
  644.         if [ -n "$trader_result" ] ; then
  645.             basename "$trader_result"
  646.             exit_success
  647.         else
  648.             exit_failure_operation_failed
  649.         fi
  650.     else
  651.         defapp_generic "$1"
  652.     fi
  653. }
  654.  
  655. [ x"$1" != x"" ] || exit_failure_syntax
  656.  
  657. mode=
  658. action=
  659. filename=
  660. mimetype=
  661.  
  662. case $1 in
  663.   install)
  664.     action=install
  665.     ;;
  666.  
  667.   uninstall)
  668.     action=uninstall
  669.     ;;
  670.  
  671.   query)
  672.     shift
  673.  
  674.     if [ -z "$1" ] ; then
  675.         exit_failure_syntax "query type argument missing"
  676.     fi
  677.  
  678.     case $1 in
  679.       filetype)
  680.         action=info
  681.  
  682.         filename="$2"
  683.         if [ -z "$filename" ] ; then
  684.             exit_failure_syntax "FILE argument missing"
  685.         fi
  686.         case $filename in
  687.           -*)
  688.             exit_failure_syntax "unexpected option '$filename'"
  689.             ;;
  690.         esac
  691.         check_input_file "$filename"
  692.         ;;
  693.  
  694.       default)
  695.         action=defapp
  696.         mimetype="$2"
  697.         if [ -z "$mimetype" ] ; then
  698.             exit_failure_syntax "mimetype argument missing"
  699.         fi
  700.         case $mimetype in
  701.           -*)
  702.             exit_failure_syntax "unexpected option '$mimetype'"
  703.             ;;
  704.  
  705.           */*)
  706.             # Ok
  707.             ;;
  708.  
  709.           *)
  710.             exit_failure_syntax "mimetype '$mimetype' is not in the form 'minor/major'"
  711.             ;;
  712.         esac
  713.         ;;
  714.  
  715.       *)
  716.       exit_failure_syntax "unknown query type '$1'"
  717.       ;;
  718.     esac
  719.     ;;
  720.  
  721.   default)
  722.     action=makedefault
  723.     shift
  724.  
  725.     if [ -z "$1" ] ; then
  726.         exit_failure_syntax "application argument missing"
  727.     fi
  728.     case $1 in
  729.       -*)
  730.         exit_failure_syntax "unexpected option '$1'"
  731.         ;;
  732.  
  733.       *.desktop)
  734.         filename="$1"
  735.         ;;
  736.  
  737.       *)
  738.         exit_failure_syntax "malformed argument '$1', expected *.desktop"
  739.         ;;
  740.     esac
  741.     ;;
  742.  
  743.   *)
  744.   exit_failure_syntax "unknown command '$1'"
  745.   ;;
  746. esac
  747.  
  748. shift
  749.  
  750.  
  751. if [ "$action" = "makedefault" ]; then
  752.     if [ -z "$1" ] ; then
  753.         exit_failure_syntax "mimetype argument missing"
  754.     fi
  755.  
  756.     while [ $# -gt 0 ] ; do
  757.         case $1 in
  758.           -*)
  759.             exit_failure_syntax "unexpected option '$1'"
  760.             ;;
  761.         esac
  762.         mimetype="$1"
  763.         shift
  764.  
  765.         make_default_kde "$filename" "$mimetype"
  766.         make_default_generic "$filename" "$mimetype"
  767.     done
  768.     exit_success
  769. fi
  770.  
  771. if [ "$action" = "info" ]; then
  772.     detectDE
  773.  
  774.     if [ x"$DE" = x"" ]; then
  775.         if [ -x /usr/bin/file ]; then
  776.             DE=generic
  777.         fi
  778.     fi
  779.  
  780.     case "$DE" in
  781.         kde)
  782.         info_kde "$filename"
  783.         ;;
  784.  
  785.         gnome)
  786.         info_gnome "$filename"
  787.         ;;
  788.  
  789.         *)
  790.         info_generic "$filename"
  791.         ;;
  792.     esac
  793.     exit_failure_operation_impossible "no method available for quering MIME type of '$filename'"
  794. fi
  795.  
  796. if [ "$action" = "defapp" ]; then
  797.     detectDE
  798.  
  799.     case "$DE" in
  800.         kde)
  801.         defapp_kde "$mimetype"
  802.         ;;
  803.  
  804.         *)
  805.         defapp_generic "$mimetype"
  806.         ;;
  807.     esac
  808.     exit_failure_operation_impossible "no method available for quering default application for '$mimetype'"
  809. fi
  810.  
  811. vendor=true
  812. while [ $# -gt 0 ] ; do
  813.     parm="$1"
  814.     shift
  815.  
  816.     case $parm in
  817.       --mode)
  818.         if [ -z "$1" ] ; then
  819.             exit_failure_syntax "mode argument missing for --mode"
  820.         fi
  821.         case "$1" in
  822.           user)
  823.             mode="user"
  824.             ;;
  825.  
  826.           system)
  827.             mode="system"
  828.             ;;
  829.  
  830.           *)
  831.             exit_failure_syntax "unknown mode '$1'"
  832.             ;;
  833.         esac
  834.         shift
  835.         ;;
  836.  
  837.       --novendor)
  838.         vendor=false
  839.         ;;
  840.  
  841.       -*)
  842.         exit_failure_syntax "unexpected option '$parm'"
  843.         ;;
  844.  
  845.       *)
  846.         if [ -n "$filename" ] ; then
  847.             exit_failure_syntax "unexpected argument '$parm'"
  848.         fi
  849.  
  850.         filename="$parm"
  851.         check_input_file "$filename"
  852.         ;;
  853.     esac
  854. done
  855.  
  856. if [ -z "$action" ] ; then
  857.     exit_failure_syntax "command argument missing"
  858. fi
  859.  
  860. if [ -n "$XDG_UTILS_INSTALL_MODE" ] ; then
  861.     if [ "$XDG_UTILS_INSTALL_MODE" = "system" ] ; then
  862.         mode="system"
  863.     elif [ "$XDG_UTILS_INSTALL_MODE" = "user" ] ; then
  864.         mode="user"
  865.     fi
  866. fi
  867.  
  868. if [ -z "$mode" ] ; then
  869.     if [ `whoami` = "root" ] ; then
  870.         mode="system"
  871.     else
  872.         mode="user"
  873.     fi
  874. fi
  875.  
  876. if [ -z "$filename" ] ; then
  877.     exit_failure_syntax "mimetypes-file argument missing"
  878. fi
  879.  
  880. if [ "$vendor" =  "true" -a "$action" = "install" ] ; then
  881.     check_vendor_prefix "$filename"
  882. fi
  883.  
  884. xdg_base_dir=
  885. xdg_dir_name=mime/packages/
  886.  
  887. xdg_user_dir="$XDG_DATA_HOME"
  888. [ -n "$xdg_user_dir" ] || xdg_user_dir="$HOME/.local/share"
  889. [ x"$mode" = x"user" ] && xdg_base_dir="$xdg_user_dir/mime"
  890. xdg_user_dir="$xdg_user_dir/$xdg_dir_name"
  891.  
  892. xdg_system_dirs="$XDG_DATA_DIRS"
  893. [ -n "$xdg_system_dirs" ] || xdg_system_dirs=/usr/local/share/:/usr/share/
  894. for x in `echo $xdg_system_dirs | sed 's/:/ /g'`; do
  895.     if [ -w $x/$xdg_dir_name ] ; then
  896.         [ x"$mode" = x"system" ] && xdg_base_dir="$x/mime"
  897.         xdg_global_dir="$x/$xdg_dir_name"
  898.         break
  899.     fi
  900. done
  901. [ -w $xdg_global_dir ] || xdg_global_dir=
  902. DEBUG 3 "xdg_user_dir: $xdg_user_dir"
  903. DEBUG 3 "xdg_global_dir: $xdg_global_dir"
  904.  
  905. # Find KDE3 mimelnk directory
  906. kde_user_dir=
  907. kde_global_dir=
  908. kde_global_dirs=`kde${KDE_SESSION_VERSION}-config --path mime 2> /dev/null`
  909. DEBUG 3 "kde_global_dirs: $kde_global_dirs"
  910. first=
  911. for x in `echo $kde_global_dirs | sed 's/:/ /g'` ; do
  912.     if [ -z "$first" ] ; then
  913.         first=false
  914.         kde_user_dir="$x"
  915.     elif [ -w $x ] ; then
  916.         kde_global_dir="$x"
  917.     fi
  918. done
  919. DEBUG 3 "kde_user_dir: $kde_user_dir"
  920. DEBUG 3 "kde_global_dir: $kde_global_dir"
  921.  
  922. # TODO: Gnome legacy support
  923. # See http://forums.fedoraforum.org/showthread.php?t=26875
  924. gnome_user_dir="$HOME/.gnome/apps"
  925. gnome_global_dir=/usr/share/gnome/apps
  926. [ -w $gnome_global_dir ] || gnome_global_dir=
  927. DEBUG 3 "gnome_user_dir: $gnome_user_dir"
  928. DEBUG 3 "gnome_global_dir: $gnome_global_dir"
  929.  
  930. if [ x"$mode" = x"user" ] ; then
  931.     xdg_dir="$xdg_user_dir"
  932.     kde_dir="$kde_user_dir"
  933.     gnome_dir="$gnome_user_dir"
  934.     my_umask=077
  935. else
  936.     xdg_dir="$xdg_global_dir"
  937.     kde_dir="$kde_global_dir"
  938.     gnome_dir="$gnome_global_dir"
  939.     my_umask=022
  940.     if [ -z "${xdg_dir}${kde_dir}${gnome_dir}" ] ; then
  941.         exit_failure_operation_impossible "No writable system mimetype directory found."
  942.     fi
  943. fi
  944.  
  945. # echo "[xdg|$xdg_user_dir|$xdg_global_dir]"
  946. # echo "[kde|$kde_user_dir|$kde_global_dir]"
  947. # echo "[gnome|$gnome_user_dir|$gnome_global_dir]"
  948. # echo "[using|$xdg_dir|$kde_dir|$gnome_dir]"
  949.  
  950. basefile=`basename "$filename"`
  951. #[ -z $vendor ] || basefile="$vendor-$basefile"
  952.  
  953. mimetypes=
  954. if [ -n "$kde_dir" ] ; then
  955.     DEBUG 2 "KDE3 mimelnk directory found, extracting mimetypes from XML file"
  956.  
  957.     mimetypes=`awk < "$filename" '
  958. # Strip XML comments
  959. BEGIN {
  960.  supress=0
  961. }
  962. {
  963.  do
  964.     if (supress) {
  965.        if (match($0,/-->/)) {
  966.            $0=substr($0,RSTART+RLENGTH)
  967.            supress=0
  968.        }
  969.        else {
  970.            break
  971.        }
  972.     }
  973.     else {
  974.        if (match($0,/<!--/)) {
  975.            if (RSTART>1) print substr($0,0,RSTART)
  976.            $0=substr($0,RSTART+RLENGTH)
  977.            supress=1
  978.        }
  979.        else {
  980.            if ($0) print $0
  981.            break
  982.        }
  983.     }
  984.  while(1)
  985. }
  986. ' | awk '
  987. # List MIME types listed in <mime-type> tags
  988. BEGIN {
  989.   RS="<"
  990. }
  991. /^mime-info/, /^\/mime-info/ {
  992.   if (match($0,/^mime-type/)) {
  993.     if (match($0,/type="[^"]*/) || match($0,/type='"'"'[^'"'"']*/)) {
  994.       print substr($0,RSTART+6,RLENGTH-6)
  995.     }
  996.   }
  997. }'`
  998. fi
  999.  
  1000. DEBUG 1 "$action mimetype in $xdg_dir"
  1001.  
  1002. case $action in
  1003.     install)
  1004.         save_umask=`umask`
  1005.         umask $my_umask
  1006.  
  1007.         for x in $xdg_dir ; do
  1008.             mkdir -p $x
  1009.             eval 'cp $filename $x/$basefile'$xdg_redirect_output
  1010.         done
  1011.  
  1012.         if [ -n "$mimetypes" ] ; then
  1013.             # No quotes around $mimetypes
  1014.             for x in $mimetypes ; do
  1015.                 DEBUG 1 "Installing $kde_dir/$x.desktop (KDE 3.x support)"
  1016.                 mkdir -p `dirname $kde_dir/$x.desktop`
  1017.                 awk < "$filename" '
  1018. # Strip XML comments
  1019. BEGIN {
  1020.  supress=0
  1021. }
  1022. {
  1023.  do
  1024.     if (supress) {
  1025.        if (match($0,/-->/)) {
  1026.            $0=substr($0,RSTART+RLENGTH)
  1027.            supress=0
  1028.        }
  1029.        else {
  1030.            break
  1031.        }
  1032.     }
  1033.     else {
  1034.        if (match($0,/<!--/)) {
  1035.            if (RSTART>1) print substr($0,0,RSTART)
  1036.            $0=substr($0,RSTART+RLENGTH)
  1037.            supress=1
  1038.        }
  1039.        else {
  1040.            if ($0) print $0
  1041.            break
  1042.        }
  1043.     }
  1044.  while(1)
  1045. }
  1046. ' | awk > $kde_dir/$x.desktop '
  1047. # Extract mimetype $x from the XML file $filename
  1048. # Note that bash requires us to escape a single quote as '"'"'
  1049. BEGIN {
  1050.   the_type=ARGV[1]
  1051.   the_source=ARGV[2]
  1052.   ARGC=1
  1053.   RS="<"
  1054.   found=0
  1055.   glob_patterns=""
  1056. }
  1057. /^mime-info/, /^\/mime-info/ {
  1058.   if (match($0,/^mime-type/)) {
  1059.     if (match($0,/type="[^"]*/) || match($0,/type='"'"'[^'"'"']*/)) {
  1060.       if (substr($0,RSTART+6,RLENGTH-6) == the_type) {
  1061.         found=1
  1062.         print "[Desktop Entry]"
  1063.         print "# Installed by xdg-mime from " the_source
  1064.         print "Type=MimeType"
  1065.         print "MimeType=" the_type
  1066.         the_icon=the_type
  1067.         sub("/", "-", the_icon)
  1068.         print "Icon=" the_icon
  1069.       }
  1070.     }
  1071.   }
  1072.   else if (found) {
  1073.     if (match($0,/^\/mime-type/)) {
  1074.       if (glob_patterns)
  1075.          print "Patterns=" glob_patterns
  1076.       exit 0
  1077.     }
  1078.  
  1079.     if (match($0,/^sub-class-of/)) {
  1080.       if (match($0,/type="[^"]*/) || match($0,/type='"'"'[^'"'"']*/)) {
  1081.         print "X-KDE-IsAlso=" substr($0,RSTART+6,RLENGTH-6)
  1082.       }
  1083.       else {
  1084.         print "Error: '"'"'type'"'"' argument missing in " RS $0
  1085.         exit 1
  1086.       }
  1087.     }
  1088.     if (match($0,/^glob/)) {
  1089.       if (match($0,/pattern="[^"]*/) || match($0,/pattern='"'"'[^'"'"']*/)) {
  1090.         glob_patterns = glob_patterns substr($0,RSTART+9,RLENGTH-9) ";"
  1091.       }
  1092.       else {
  1093.         print "Error: '"'"'pattern'"'"' argument missing in " RS $0
  1094.         exit 1
  1095.       }
  1096.     }
  1097.     if (match($0,/^comment/)) {
  1098.       if (match($0,/xml:lang="[^"]*/) || match($0,/xml:lang='"'"'[^'"'"']*/)) {
  1099.         lang=substr($0,RSTART+10,RLENGTH-10)
  1100.       }
  1101.       else {
  1102.         lang=""
  1103.       }
  1104.       if (match($0,/>/)) {
  1105.         comment=substr($0,RSTART+1)
  1106.         sub("<", "<", comment)
  1107.         sub(">", ">", comment)
  1108.         sub("&", "\\&", comment)
  1109.         if (lang)
  1110.            print "Comment[" lang "]=" comment
  1111.         else
  1112.            print "Comment=" comment
  1113.       }
  1114.     }
  1115.   }
  1116. }
  1117. END {
  1118.   if (!found) {
  1119.     print "Error: Mimetype '"'"'" the_type "'"'"' not found"
  1120.     exit 1
  1121.   }
  1122. }
  1123. ' $x $basefile
  1124.                 if [ "$?" = "1" ] ; then
  1125.                     grep -A 10 "^Error:" $kde_dir/$x.desktop >&2
  1126.                     rm $kde_dir/$x.desktop
  1127.                     exit 1
  1128.                 fi
  1129.             done
  1130.         fi
  1131.  
  1132.         umask $save_umask
  1133.         ;;
  1134.  
  1135.     uninstall)
  1136.         for x in $xdg_dir ; do
  1137.             rm -f $x/$basefile
  1138.         done
  1139.  
  1140.         # No quotes around $mimetypes
  1141.         for x in $mimetypes ; do
  1142.             if grep '^# Installed by xdg-mime' $kde_dir/$x.desktop >/dev/null 2>&1; then
  1143.                 DEBUG 1 "Removing $kde_dir/$x.desktop (KDE 3.x support)"
  1144.                 rm -f $kde_dir/$x.desktop
  1145.             fi
  1146.         done
  1147.         ;;
  1148. esac
  1149.  
  1150. update_mime_database $xdg_base_dir
  1151.  
  1152. exit_success
  1153.  
  1154.